﻿var DeliveryAddrMgr = {
    addrList: "ss.del.addr.list",
    defIndex: "ss.del.addr.index",
    editIndex: "ss.del.addr.edit",
    invalidDefIndex: -1,
    addDeliveryAddrOp: 1,
    delDeliveryAddrOp: 2,
    setDeliveryAddrList: function(deliveryAddrList) {
        setStorage(DeliveryAddrMgr.addrList, deliveryAddrList);
    },
    getDeliveryAddrList: function() {
        return getStorageVal(DeliveryAddrMgr.addrList);
    },
    setDeliveryAddrIndex: function (defIndex) {
        setStorage(DeliveryAddrMgr.defIndex, defIndex);
    },
    getDeliveryAddrIndex: function () {
        var defIndex = DeliveryAddrMgr.invalidDefIndex;
        var defIndexVal = getStorageVal(DeliveryAddrMgr.defIndex);
        if (null != defIndexVal) {
            defIndex = Number(defIndexVal);
        }
        return defIndex;
    },
    getDefDeliveryAddr: function (defIndex) {
        var deliveryAddr = null;
        if (defIndex == DeliveryAddrMgr.invalidDefIndex) {
            return deliveryAddr;
        }
        var deliveryAddrList = getStorageVal(DeliveryAddrMgr.addrList);
        if (null != deliveryAddrList) {
            var size = deliveryAddrList.length;
            if (defIndex < size) {
                deliveryAddr = deliveryAddrList[defIndex];
            }
        }
        return deliveryAddr;
    },
    removeDeliveryAddr: function (addrId) {
        var deliveryAddrList = getStorageVal(DeliveryAddrMgr.addrList);
        if (null != deliveryAddrList) {
            var bChange = false;
            for (addr in deliveryAddrList) {
                if (addrId == deliveryAddrList[addr].id) {
                    deliveryAddrList.splice(addr, 1);
                    bChange = true;
                    break;
                }
            }
            if (true == bChange) {
                setStorage(DeliveryAddrMgr.addrList, deliveryAddrList);
                var size = deliveryAddrList.length;
                var defIndex = DeliveryAddrMgr.getDeliveryAddrIndex();
                if (defIndex >= size) {
                    defIndex = size - 1;
                    DeliveryAddrMgr.setDeliveryAddrIndex(defIndex);
                }
            }
        }
    },
    changeDeliveryAddr: function (addrInfo) {
        var deliveryAddrList = getStorageVal(DeliveryAddrMgr.addrList);
        var bFound = false;
        for (addr in deliveryAddrList) {
            if (addrInfo.id == deliveryAddrList[addr].id) {
                deliveryAddrList[addr] = addrInfo;
                bFound = true;
                break;
            }
        }
        if (false == bFound) {
            deliveryAddrList.push(addrInfo);
        }
        setStorage(DeliveryAddrMgr.addrList, deliveryAddrList);
    },
    setEditDeliveryIndex: function (editIndex) {
        setStorage(DeliveryAddrMgr.editIndex, editIndex);
    },
    getEditDeliveryAddr: function () {
        var deliveryAddr = null;
        var editIndexVal = getStorageVal(DeliveryAddrMgr.editIndex);
        if (null != editIndexVal) {
            var editIndex = Number(editIndexVal);
            if (editIndex > DeliveryAddrMgr.invalidDefIndex) {
                var deliveryAddrList = getStorageVal(DeliveryAddrMgr.addrList);
                if (null != deliveryAddrList) {
                    var size = deliveryAddrList.length;
                    if (editIndex < size) {
                        deliveryAddr = deliveryAddrList[editIndex];
                    }
                }
            }
        }
        return deliveryAddr;
    },
    resetDeliveryAddrMgr: function () {
        removeStorage(DeliveryAddrMgr.addrList);
        removeStorage(DeliveryAddrMgr.defIndex);
        removeStorage(DeliveryAddrMgr.editIndex);
    },
    loadDeliveryListAddr: function (showDeliveryAddrListCallback) {
        var deliveryAddrList = DeliveryAddrMgr.getDeliveryAddrList();
        var defIndex = DeliveryAddrMgr.getDeliveryAddrIndex();
        if (null == deliveryAddrList || defIndex == DeliveryAddrMgr.invalidDefIndex) {
            DeliveryAddrMgr.getDeliveryAddrListFromService(showDeliveryAddrListCallback);
        } else if (null != showDeliveryAddrListCallback) {
            showDeliveryAddrListCallback(deliveryAddrList, defIndex);
        }
    },
    getDeliveryAddrListFromService: function (showDeliveryAddrListCallback) {
        var sessionId = UserInfoMgr.getSessionId();
        var addrListRequestJson = {
            "session_id": sessionId,
            "from": 0,
            "number": 0
        };
        doAjax(addrListRequestJson, "delivery_addr_history", function (msg) {
            if (msg.result == "0") {
                DeliveryAddrMgr.setDeliveryAddrList(msg.addr_set);
                DeliveryAddrMgr.setDeliveryAddrIndex(msg.defaut_index);
                if (null != showDeliveryAddrListCallback) {
                    showDeliveryAddrListCallback(msg.addr_set, msg.defaut_index);
                }
            }
        });
    },
    saveDeliveryAddress: function (saveDeliveryAddrCallback) {

    },
    saveDeliveryAddr: function (deliveryAddr, saveDeliveryAddrCallback) {
        var sessionId = UserInfoMgr.getSessionId();
        var saveAddressJson = {
            "type": DeliveryAddrMgr.addDeliveryAddrOp,
            "info": deliveryAddr,
            "session_id": sessionId
        };
        doAjax(saveAddressJson, "delivery_addr_op", function (result) {
            if (result.result == "0") {
                DeliveryAddrMgr.changeDeliveryAddr(deliveryAddr);
                turnBack();  //保存成功后返回到地址管理
            } else {
                if (null != saveDeliveryAddrCallback) {
                    saveDeliveryAddrCallback(result.msg);
                }
            }
        });
    },
    deleteDeliveryAddr: function (deliveryAddrId, deleteDeliveryAddrCallback) {
        var sessionId = UserInfoMgr.getSessionId();
        var deleteDeliveryAddrJson = {
            "type": DeliveryAddrMgr.delDeliveryAddrOp,
            "info": { "id": deliveryAddrId, },
            "session_id": sessionId
        };
        doAjax(deleteDeliveryAddrJson, "delivery_addr_op", function (msg) {
            if (msg.result == "0") {
                DeliveryAddrMgr.removeDeliveryAddr(deliveryAddrId);
                if (null != deleteDeliveryAddrCallback) {
                    deleteDeliveryAddrCallback(deliveryAddrId);
                }
            }
        });
    }
};